package edu.sctu.yueding.activitys;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.xys.libzxing.zxing.encoding.EncodingUtils;

import edu.sctu.yueding.R;
import edu.sctu.yueding.config.CacheConfig;
import edu.sctu.yueding.config.URLConfig;
import edu.sctu.yueding.utils.ImageLoader;
import edu.sctu.yueding.utils.httputils.GetVolleyHttp;
import edu.sctu.yueding.utils.httputils.SharePreference;
import edu.sctu.yueding.view.CircleImageView;

/**
 * Created by 鑫爷您慢点 on 2016/8/2.
 */
public class MyQRCodeActivity extends BaseActivity {

    private TextView mBackTxt;
    private ImageView mMyQrCodeImageView;

    private Bitmap portrait;
    private SharePreference sharePreference;
    private CircleImageView circleImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_qrcode);
        initSystemBar(this, 0);
        initView();
    }

    private void initView() {
        mBackTxt = (TextView) findViewById(R.id.id_my_qrcode_back_txt);
        mMyQrCodeImageView = (ImageView) findViewById(R.id.id_my_qrcode_img);
        sharePreference = SharePreference.getInstance(this);
        circleImageView = (CircleImageView) findViewById(R.id.id_my_qr_user_portrait);
//        GetVolleyHttp.getInstance(MyQRCodeActivity.this).setImageLoader(circleImageView, R.drawable.user_default_portrait, R.drawable.error_image, sharePreference.getString(CacheConfig.CACHE_PORTRAIT_PATH));

        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/悦定/";
        Bitmap bt = BitmapFactory.decodeFile(path + sharePreference.getString(CacheConfig.CACHE_USER_NAME) + "head.jpeg");//从Sd中找头像，转换成Bitmap

        if (bt != null) {

            @SuppressWarnings("deprecation")
            Drawable drawable = new BitmapDrawable(bt);//转换成drawable
            circleImageView.setImageDrawable(drawable);
        }else {

//            circleImageView.setBackgroundResource(R.drawable.user_default_portrait);
        }

        mBackTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Bitmap bitmap = EncodingUtils.createQRCode(URLConfig.getMyQrcodeUrl(sharePreference.getLong(CacheConfig.CACHE_USER_ID)), 800, 800, null);
        mMyQrCodeImageView.setImageBitmap(bitmap);

    }
}
